file_list = dir([out_path '/*.pshape']);
for i_file =1:size(file_list,1)
    source_name=[out_path '/' file_list(i_file).name];
    ind_img = sscanf(file_list(i_file).name,'image_%d_shape_%d_deform_%d_.pshape')+1;
    ind_img = ind_img(1);
    load([cachePath '/S2maps_' num2str(ind_img) '.mat']);
    load([cachePath '/S1maps_' num2str(ind_img) '.mat']);

    %% parse line file, convert format for libFilter
    all_lines= [];
    p_file=fopen(source_name);
    if p_file==-1
        fprintf('falied to load image %s',source_name);
    end
    text = fgetl(p_file);
    data= sscanf(text,'%f, ');
    width = data(1); height = data(2);
    while(~feof(p_file))
        text = fgetl(p_file);
        data = sscanf(text,'%f, ');
        switch data(1)
            case 1
                center_row = 0.5*(data(3)+data(5))+1;
                center_col = 0.5*(data(2)+data(4))+1;
                theta = atan(-(data(3)-data(5))/(data(2)-data(4)));
		if data(2)==data(4)
		    theta = pi/2;
		end
                orient = round(theta/pi*nOrient);
				if orient<0
					orient = orient + nOrient;
				end
                line_length = sqrt((data(2)-data(4))^2+(data(3)-data(5))^2);
                line_length = round(((line_length/(2*h+1))-1)/2);
                line_length = max(line_length,0); 
                line_length = min(line_length,nLength-1);
                % orient angle length x y score
                line = [orient 0 line_length center_row center_col 0];
                line = round(line);
                all_lines = [all_lines;line];
            otherwise
                frpintf('unkonw shape type found!\n');
        end
    end
    fclose(p_file);
    
    %% get arg-max through c back max 2

    all_deformed_lines = zeros(size(all_lines'));
    n_lines = size(all_lines,1);
    CArgMax2Single(1,nOrient,nLength,1,S2map,h,...
        Lrange2,Orange2,0,height,width,...
        n_lines,all_lines',all_deformed_lines);
    all_deformed_lines = all_deformed_lines';
    %% convert the defomred lines back to start and end point, draw them
    line_length =  ((all_deformed_lines(:,3)*2)+1)*(2*h+1)/2;

    d_s_x = all_deformed_lines(:,4) - line_length.*sin(all_deformed_lines(:,1)/nOrient*pi);  % deformed line, start x
    d_s_y = all_deformed_lines(:,5) + line_length.*cos(all_deformed_lines(:,1)/nOrient*pi);
    
    d_e_x = all_deformed_lines(:,4) + line_length.*sin(all_deformed_lines(:,1)/nOrient*pi);
    d_e_y = all_deformed_lines(:,5) - line_length.*cos(all_deformed_lines(:,1)/nOrient*pi);
    
    
    img = zeros(height,width,'uint8');
    for i_line = 1:n_lines
        mask=lineMask([height width],[d_s_x(i_line) d_s_y(i_line)], [d_e_x(i_line) d_e_y(i_line)],1);
        img = max(img, mask);
    end
    out_name = [source_name '.S2_mask.png'];
    imwrite(255-img,out_name);
    %% draw deformed gabors in sum_1 map
    sym = zeros(height,width,'single');
    asym = cell(1);
    asym{1}=zeros(height,width,'single');
    dummy_response = zeros(1000,1,'single');
    for i_line = 1:n_lines
  	if (d_s_x(i_line)<0 || d_s_x(i_line)>=height || d_s_y(i_line)<0 || d_s_y(i_line)>=width)
 		continue;
	end
	if (d_e_x(i_line)<0 || d_e_x(i_line)>=height || d_e_y(i_line)<0 || d_e_y(i_line)>=width)
		continue;
	end
       CArgMax1andInhibition(1,nOrient,nLength,1,S1map,...
            C,height,width,h,Lrange,Orange,...
            all_lines(i_line,:),all_deformed_lines(i_line,:),dummy_response,...
            sym,asym,allsymbol);
    end
    sym = uint8((1-sym)*255);
    asym{1} = uint8((1-asym{1})*255);
    out_name = [source_name '.S2.png'];
    imwrite(sym,out_name);
    out_name = [source_name '.S1.png'];
    imwrite(asym{1},out_name);
    
    gray_img =  I_gray{ind_img};
    out_name = [source_name '.S2.impose.png'];
    imwrite(0.5*sym+0.5*gray_img,out_name);
    out_name = [source_name '.S1.impose.png'];
    imwrite(0.5*asym{1}+0.5*gray_img,out_name);
	clear S1map
	clear S2map
end
